package cn.phub.org.controller;


import cn.phub.basic.util.AjaxResult;
import cn.phub.basic.util.PageList;
import cn.phub.org.domain.DictionaryDetail;
import cn.phub.org.query.DictionaryQuery;
import cn.phub.org.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    /*
    * 查询所有数据
    * */
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return detailService.queryAll();
    }
    /*
    * 高级查询+分页,查询所有数据
    * */
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryQuery query) {
        System.err.println(query.tpId); // null
        return detailService.queryPage(query);
    }
    /*
     * 添加修改
     * */
    @PutMapping
    //@RequestBody从请求体中拿数据
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        System.err.println("______________________========" + dictionaryDetail);
        try {
            if(dictionaryDetail.getId() == null){
                detailService.add(dictionaryDetail);
            }else{
                detailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /*
     * 删除
     * */
    @DeleteMapping("/{id}")
    //@PathVariable 用来获取请求地址中占位符的值
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            detailService.remove(id);
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("访问人数过多，系统崩溃了");
        }
    }
    /*
     * 批量删除
     * */
    @PatchMapping
    //@PathVariable 用来获取请求地址中占位符的值
    public AjaxResult batchRemove(@RequestBody List<Long>  ids){
        try {
            detailService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("访问人数过多，系统崩溃了");
        }
    }
    /*
     * 查询一条数据
     * */
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return detailService.queryById(id);
    }

}
